export default class cookie {
  // 设置cookie
  static toRawType (value) {
    return Object.prototype.toString.call(value).slice(8, -1)
  }
  // map{data,maxAge,domain,path}
  static setCookie(map) {
    if(this.toRawType(map.data) != 'Object'  ){
      console.error('type of params is not Object')
      return
    }
    if(this.toRawType(map.data) != 'Object'  ){
      console.error('type of data is not Object')
      return
    }
    if(Object.keys(map.data).length == 0){
      return
    }
    if(!map.hasOwnProperty('maxAge')){
      map.maxAge = 60*60*24*31
    }
    if(!map.hasOwnProperty('domain')){
      map.domain = window.document.domain
    }
    if(!map.hasOwnProperty('path')){
      map.path = '/'
    }
    for(let key in map.data){
      document.cookie = key + "=" + escape(map.data[key]) + ";max-age=" + map.maxAge + ";domain=" + map.domain + ";path=" + map.path;
    }
  }

  // 读取cookie
  static getCookie(name) {
    if (document.cookie.length > 0) {
      let start = document.cookie.indexOf(name + "=")
      if (start != -1) {
        start = start + name.length + 1
        let end = document.cookie.indexOf(";", start)
        if (end == -1)
          end = document.cookie.length
        return unescape(document.cookie.substring(start, end))
      }
    }
    return ""
  }

}
